from utils.sqlutils import SqlUtils
from sqlmap.adminsqlmap import SqlMap as AdminSqlMap


class Admin:
    def __init__(self):
        self.sqlutils = SqlUtils()

    def admin_list(self,sqlmap,**kwargs):
        data = self.sqlutils.execute(AdminSqlMap.get(sqlmap).format(**kwargs))
        admin_list = []
        for row in data.get("content"):
            admin = {}
            admin["id"] = row[0]
            admin["username"] = row[1]
            admin["password"] = row[2]
            admin["email"] = row[3]
            admin_list.append(admin)
        return admin_list

    def admin_info(self):
        return self.sqlutils.execute(AdminSqlMap.get("info"))

    def admin_create(self, **kwargs):
        return self.sqlutils.execute(AdminSqlMap.get("create").format(**kwargs))

    def admin_delete(self, **kwargs):
        return self.sqlutils.execute(AdminSqlMap.get("delete").format(**kwargs))

    def admin_edit(self, **kwargs):
        return self.sqlutils.execute(AdminSqlMap.get("edit").format(**kwargs))
    
    def admin_query(self, **kwargs):
        sql = AdminSqlMap.get("query").format(**kwargs)
        data = self.sqlutils.execute(sql)

        admin_list = []
        for row in data.get("content"):
            admin = {}
            admin["id"] = row[0]
            admin["username"] = row[1]
            admin["password"] = row[2]
            admin["email"] = row[3]
            admin_list.append(admin)
        return admin_list


if __name__ == '__main__':
    admin = Admin()
    admin.admin_create(username="mx",image_path="./storage/mx.jpg",viewname="孟翔")
    # print(user.user_list())
